##########################################
#
# Copyright 2008 David B. Spadea
# All rights reserved
#
# http://www.spadea.net
#
# This program and all supporting code  
# is released under the terms of the 
# GNU General Public License version 3.
#
##########################################
package BatchTools::Config;

use base qw(Exporter);

use vars (@EXPORT_OK);

@EXPORT_OK = qw(
	$CONFIG
	getConfig
);

use YAML qw(freeze thaw);

use FindBin qw($Bin);

my $BASEDIR = "$Bin/../../";
my $CONFDIR = "$BASEDIR/common/etc";

my $CONFIG;

&readConfig;

sub getConfig { return $CONFIG; }

sub readConfig
{
	if ( ! -e "$CONFDOR/config.yml")
	{
		&createDefaultConfig;
	}

	open (CONFIG, "<$CONFDIR/config.yml")
		or die ("Unable to open $CONFDIR/config.yml");

	my $conf = join("", <CONFIG>);

	close CONFIG;

	$CONFIG = thaw($conf);

	return 1;
}

sub createDefaultConfig
{
	$CONFIG = {
		Database => {
			  Driver => "Pg"
			, Host => "localhost"
			, UserID => "postgres"
			, Password => "postgres"
			, DBName => "batchtools"
		}
	};

	my $conf = freeze($CONFIG);

	open (CONFIG, ">$CONFDIR/config.yml")
		or die ("Unable to open $CONFDIR/config.yml for writing!");
	
	print CONFIG $conf;

	close CONFIG;

	return 1;
}

1;
